# coding=utf-8
import os
import re
import requests

URL_LIST = []
IMG_LIST = []
BASE_URL = "https://www.mzitu.com/all"
Hostreferer = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
    'Referer': 'http://www.mzitu.com'
}
Picreferer = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
    'Referer': 'http://i.meizitu.net'
}


def main():
    find_url_list()
    find_img_list()


def find_url_list():
    content = requests.get(BASE_URL, headers=Hostreferer).text
    reg = r'<a href="https://www.mzitu.com/.*? target="_blank">'
    pa = re.compile(reg)
    url_list = pa.findall(content)
    for ul in url_list:
        s = re.search(r'https://www.mzitu.com/[0-9a-z]*', ul).group()
        if s not in URL_LIST:
            URL_LIST.append(s)


def find_img_list():
    for ul in URL_LIST:
        title = ''
        count = 0
        content = requests.get(ul, headers=Hostreferer).text.replace("\n", "").replace("\r", "")

        # 获取图集
        r = re.search(r'<h2 class="main-title">.*?</h2>', content)
        if r is not None:
            title = r.group().replace("<h2 class=\"main-title\">", "").replace("</h2>", "")

        # 创建图集
        path = "img/" + title
        if not os.path.exists(path):
            os.makedirs(path)

        # 获取页数
        r = re.search(r'<div class="pagenavi">.*?</div>', content)
        if r is not None:
            m = re.findall(r"<span>[\d|\d\d].*?</span>", r.group())[-1].replace("<span>", "").replace("</span>", "")
            count = int(m)

        i = 1
        while i <= int(count):
            u = ul + '/' + str(i)
            get_img(u, title, i)
            i += 1


def get_img(ul, title, index):
    content = requests.get(ul, headers=Hostreferer).text
    r = re.search(r'<img src="https://i.meizitu.net.*?.jpg', content)
    if r is not None:
        img_url = r.group().replace("<img src=\"", "")
        if img_url not in IMG_LIST:
            IMG_LIST.append(img_url)
            save_img(img_url, title, index)
            print(img_url + " \t " + title + "-" + str(index) + " \t 保存图片成功")


def save_img(img_url, title, index):
    req = requests.get(img_url, headers=Picreferer)
    with open("img/" + title + "/" + title + "-" + str(index) + '.jpg', 'wb') as f:
        f.write(req.content)


if __name__ == '__main__':
    main()
